package com.example.allshengmedical.base;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import com.example.allshengmedical.util.LoadingDialog;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by WillJoe on 16/8/24.
 */
public abstract class BaseFragment extends Fragment {

	LoadingDialog progressDialog = null;

	// 绑定布局
	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return inflater.inflate(getlayout(), null);
	}

	// 绑定组件
	// @Override
	// public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
	// {
	// super.onViewCreated(view, savedInstanceState);
	// initView();
	// smartInject(getView());
	// progressDialog = new LoadingDialog();
	// }

	//
	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		smartInject(getView());
		initView();
		dataView();
		progressDialog = new LoadingDialog();
	}

	public abstract int getlayout();

	protected abstract void initView();

	protected abstract void dataView();

	// 方便绑定布局的
	protected <T extends View> T BindView(int id) {
		return (T) getView().findViewById(id);
	}

	public void showProgressDialog(String message, boolean flag) {
		if (progressDialog == null)
			return;
		setProgressDialogMessage(message);
		progressDialog.show(getActivity().getFragmentManager(), message);
		progressDialog.setCancelable(flag);

	}

	public void setProgressDialogMessage(String message) {
		progressDialog.setMsg(message);
	}

	public void closeProgressDialog() {
		if (progressDialog != null) {
			if (progressDialog.getDialog().isShowing()) {
				progressDialog.dismiss();
			}
		}
	}

	protected void smartInject(View view) {
		try {
			Class<? extends Fragment> clz = getClass();
			while (clz != BaseFragment.class) {
				Field[] fs = clz.getDeclaredFields();
				Resources res = getResources();
				String packageName = getActivity().getPackageName();
				for (Field field : fs) {
					if (!View.class.isAssignableFrom(field.getType())) {
						continue;
					}
					int viewId = res.getIdentifier(field.getName(), "id",
							packageName);
					if (viewId == 0)
						continue;
					field.setAccessible(true);
					try {
						View v = view.findViewById(viewId);
						field.set(this, v);
						Class<?> c = field.getType();
						Method m = c.getMethod("setOnClickListener",
								android.view.View.OnClickListener.class);
						m.invoke(v, this);
					} catch (Throwable e) {
					}
					field.setAccessible(false);

				}

				clz = (Class<? extends Fragment>) clz.getSuperclass();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
